<?php

namespace App\Http\Services;
/**
 * Created by PhpStorm.
 * User: thorjiang
 * Date: 2018/5/10
 * Time: 14:40
 */

use App\Models\AdminModel;
use App\Models\BusinessModel;
use App\Models\Meeting;
use App\Models\UserModel;
use App\Models\UserTicketModel;
use App\Models\TicketModel;
use PHPExcel;
use PHPExcel_IOFactory;

class UserMeetingDTOBuilderService
{
    /**
     * buildTickets
     * @param $userId
     * @param $userickets
     * @return array
     */
    public static function buildMeeting($userMeetings)
    {
        $data = [];
        $ids = array_column($userMeetings, 'meeting_id');
        $meetings = Meeting::whereIn('id', $ids)->get(['id','name', 'address', 'city_name', 'start_time', 'end_time', 'tel', 'owner', 'owner2'])->toArray();
        $meetingsMap = arrToMap($meetings, 'id');
        foreach ($userMeetings as $userMeeting) {
            $userTicket = static::build($userMeetings);
            $id = $userMeeting['meeting_id'];
            $meeting = isset($meetingsMap[$id]) ? $meetingsMap[$id] : null;
            $userMeeting['meeting'] = $meeting;

            $data[] = $userMeeting;
        }

        return $data;
    }

    public static function build($userMeeting)
    {
        return $userMeeting;
    }

    public static function export($userMeetings, $filename = '/storage/export.xls')
    {
        $userMeetings = static::buildMeeting($userMeetings);
        // excel列  姓名 单位 套餐 联系方式 email 是否核销, 会议名称 , 类型  验证码
        $obj = new PHPExcel();
        $obj->setActiveSheetIndex(0);
        $obj->getActiveSheet()->setTitle('名单导出');

        $obj->getActiveSheet()->setCellValue('A1', '姓名');
        $obj->getActiveSheet()->setCellValue('B1', '单位');
        $obj->getActiveSheet()->setCellValue('C1', '套餐');
        $obj->getActiveSheet()->setCellValue('D1', '联系方式');
        $obj->getActiveSheet()->setCellValue('E1', 'email');
        $obj->getActiveSheet()->setCellValue('F1', '会议名称');
        $obj->getActiveSheet()->setCellValue('G1', '是否核销');
        $obj->getActiveSheet()->setCellValue('H1', '类型');
        $obj->getActiveSheet()->setCellValue('I1', '验证码');
        $obj->getActiveSheet()->setCellValue('J1', '报名时间');

        $l = 1;
        $map = [
            'A' => 'name',
            'B' => 'company',
            'C' => 'packages',
            'D' => 'mobile',
            'E' => 'email',
            'G' => 'status',
            'F' => 'meeting.name',
            'H' => 'type',
            'I' => 'code',
            'J' => 'created_at',
        ];
        foreach ($userMeetings as $item) {
            $l++;
            foreach ($map as $k => $v) {
                if ($k == 'F') {
                    $v = $item['meeting']['name'];
                } elseif ($k == 'G') {
                    $v = ($item['status'] == 0) ? '未核销' : '已核销';
                } elseif ($k == 'H') {
                    $v = ($item['type'] == 10) ? '报名' : '参展';
                } elseif ($k == 'I' || $k == 'D') {
                    $obj->getActiveSheet()->setCellValue($k . $l, $item[$v], true)->setDataType();
                    continue;
                } else {
                    $v = (string) $item[$v];
                }

                $obj->getActiveSheet()->setCellValue($k . $l, $v);
            }
        }

        $objWriter = PHPExcel_IOFactory::createWriter($obj, 'Excel2007');
        $objWriter->save('.' . $filename);
        //$objWriter->save('1.xlsx');
        return env('APP_URL') . $filename ;
    }
}
